function edit_task(event)
{
	event.stopPropagation();
	var thisRow = 	$(event.currentTarget).parents('tr');
	var $list = $(event.currentTarget).parents('div.list');
	var listIndex = $list.parent().children('div.list').index($list);
	var rowIndex = thisRow.parent().children().index(thisRow);
	
	console.log(thisRow);
	var thisTask =  thisRow.data('task');	//tasks[listIndex][rowIndex-1];
	var project_id = $list.find('input.project_id').val();
	var task_id = thisTask.id;
	
	thisTask.users = users;
		
	thisRow.hide();
	$('#tmpl_edit_task').tmpl(thisTask).insertBefore(thisRow);
	var actEditRow = thisRow.prev();
	actEditRow.find('option#aui-'+thisTask.assign_user_id).attr('selected', 'selected');
	$('button.save').click(function(e){
		e.preventDefault();
		var ob = $(e.currentTarget).parents('form').serializeArray();
		console.log(ob);
		$.ajax({
			url: '/ajax_task/edit_task/'+project_id+'?task_id='+task_id,
			type: 'POST',
			data: ob,
			success: function(ret) {
				var data = $.parseJSON(ret);
				if (data.success)
				{
					thisTask.name = actEditRow.find('input.name').val();
					thisTask.description = actEditRow.find('input.description').val();
					thisTask.assign_user_id = actEditRow.find('select.assign_user option:selected').val();
					thisTask.assign_user_displayname = actEditRow.find('select[name="assign_user_id"] option:selected').text();
					thisTask.deadline = actEditRow.find('input.deadline').val();
					thisTask.priority = actEditRow.find('select[name="priority"] option:selected').val();
					thisTask.status_label = assign_status[thisTask.status];
					thisTask.priority_label = assign_priority[thisTask.priority];
					$('#tmpl_new_task_in_group').tmpl(thisTask).insertAfter(thisRow);
					thisRow.next().bind_process_task_row(thisTask);
					thisRow.remove();
					actEditRow.remove();
				}
			},
			error: function() {
				actEditRow.remove();
				thisRow.show();
			},
			complete: function() {
			}
		});
	});
	$('a.cancel').click(function(e){
		var editRow = $(this).parents('tr');
		editRow.remove();
		thisRow.show();
		return false;
	});
	return false;
}
function delete_task(event)
{
	event.stopPropagation();
	var list = $(event.currentTarget).parents('.list-main-body');
	var row = $(event.currentTarget).parents('tr');
	console.log(list);
	var project_id = list.find('input.project_id').val();
	var task_id = list.find('input.close-checkbox').val();
	row.hide();
	row.after('<td>...Loading...</td>');
	$.ajax({
		url: '/ajax_task/delete_task/'+project_id+'?task_id='+task_id,
		success: function(ret) {
				var data = $.parseJSON(ret);
				if (data.success)
				{
					row.next().remove();
					row.remove();
				}
		},
		error: function() {
			row.next().remove();
			row.show();
			alert('task delete error');
		},
		complete: function() {
		}
	});		
}
function add_task(e)
{
	e.stopPropagation();
	e.preventDefault();
	alert('yeah');
	var tr = $(e.currentTarget).parents('tr');
	var list_id = tr.parents('table').find('input.list_id').val();
	var project_id = tr.parents('table').find('input.project_id').val();
	
//	if (div.find('.task_name').val().length == 0 ||
//			div.find('.project_name').val().length == 0 ||
//			div.find('.assign_user').val().length == 0 ||
//			div.find('.task_priority').val().length == 0 ||
//			div.find('.deadline').val().length == 0 ||
//			div.find('.task_description').val().length == 0) {
//		return false;
//	}

	var ob = $(e.currentTarget).parents('form').serializeArray();
	//change button to loadding
	$('input.add_button').attr('disabled',true);
	$('input.add_button').attr('value','Loading');
	var str = '/ajax_task/add_task/'+project_id+'?list_id='+list_id;
	$.ajax({
		url: str,
		type: 'POST',
		data: ob,
		success: function(ret) {
				var ret = $.parseJSON(ret);
				if (ret.success)
				{
					var data = ret.data;
					data.user_displayname = tr.find('input.user_displayname').val();
					data.project_id = project_id;
					data.project_name = tr.find('input.project_name');
					data.assign_user = tr.find('assign_user').val();
					data.assign_user_displayname = tr.find('select.assign_user_id option:selected').text();
					data.assign_task_status = '11';
					console.log(data);
					add_new_task(data, tr);
					
//					var element = new Object();
//					element = {
//							id: data.id,
//							user_displayname: div.find('input.user_displayname').val(),
//							project_id: project_id,
//							project_name: div.find('input.project_name'),
//							name: div.find(".task_name").val(),
//							assign_user:div.find(".assign_user").val(),
//							project_id:div.find(".project_name").val(), 
//							priority:div.find('.task_priority').val(),
//							deadline: div.find('.deadline').val(),
//							description: div.find('.task_description').val(),
//							assign_user_displayname: div.find('select.assign_user option:selected').text(),
//							assign_task_status: '11'
//					};
//					add_new_task(element, div.parent());
				}
		},
		error: function() {
			$('input.add_button').attr('disabled',false);
			alert('Error occured!');
		},
		complete: function() {
			$('input.add_button').attr('disabled', false);
			$('input.add_button').attr('value','Add');
			tr.find(".task_name").attr('value','');
			tr.find(".task_description").attr('value','');
			tr.find(".deadline").attr('value','');
		}
	});
	return false;	
}
function add_task_none_list(e, user_id)
{
	e.stopPropagation();
	e.preventDefault();
	alert('yeah');
	var tr = $(e.currentTarget).parents('tr');
	var project_id = tr.parents('table').find('input.project_id').val();
	var ob = $(e.currentTarget).parents('form').serializeArray();
	//change button to loadding
	$('input.add_button').attr('disabled',true);
	$('input.add_button').attr('value','Loading');
	var str = '/ajax_task/add_task/'+project_id+'?user_id='+user_id;
	$.ajax({
		url: str,
		type: 'POST',
		data: ob,
		success: function(ret) {
				var ret = $.parseJSON(ret);
				if (ret.success)
				{
					var data = ret.data;
					data.user_displayname = tr.find('input[name="user_displayname"]').val();
					data.project_id = project_id;
					data.project_name = tr.find('input.project_name');
					data.assign_user = tr.find('assign_user').val();
					data.assign_user_displayname = tr.find('select.assign_user_id option:selected').text();
					data.assign_task_status = 11;
					data.status_label = assign_status[data.status];
					data.priority_label = assign_priority[data.priority];
					var $group = tr.parents('.tstyle_content').find('.list.'+ data.group_id);
					if ($group.length)
						add_new_task_in_group(data, tr, data.group_id);
					else
						add_task_with_group(data, tr, {name: tr.find('input[name="group_name"]').val(), id : data.group_id});
				}
		},
		error: function() {
			$('input.add_button').attr('disabled',false);
			alert('Error occured!');
		},
		complete: function() {
			$('input.add_button').attr('disabled', false);
			$('input.add_button').attr('value','Add');
			tr.find(".task_name").attr('value','');
			tr.find(".task_description").attr('value','');
			tr.find(".deadline").attr('value','');
		}
	});
	return false;	
}
function close_task(event)
{
	event.stopPropagation();
	var task_id = $(event.currentTarget).val();
	var row = $(event.currentTarget).parents('tr');
	var list = $(event.currentTarget).parents('.list-main-body');
	var html = row.html();
	var project_id = list.find('input.project_id').val();
	row.hide();
	var str = '/ajax_task/'+ ((row.hasClass('rowB')) ? 'close_task' : 'open_task')+'/'+project_id+'?task_id='+task_id;
	row.after('<td>...Loading...</td>');
	$.ajax({
		url: str,
		success: function(ret) {
				var data = $.parseJSON(ret);
				if (data.success)
				{
					row.next().remove();
					row.show();
					if (row.hasClass('rowB'))
					{
						row.removeClass('rowB');
						row.addClass('rowC');
					}
					else
					{
						row.removeClass('rowC');
						row.addClass('rowB');
					}
				}
		},
		error: function() {
			row.empty().append(html);
		},
		complete: function() {
		}
	});	
}
$('table.add-task-sep').ready(function(){
	//hide, who add task form
	$('.top_control .left a').click(function(e){
		$(this).parents('.top_control').hide();
		$(this).parents('#content').find('.tstyle_content div.list:first').show();
		$(this).parents('#content').find('.tstyle_content div.list:first table').show();
		return false;
	});
	
	$('table.add-task-sep a.cancel').click(function(e){
		$(this).parents('.list').hide();
		$(this).parents('#content').find('.top_control').show();
	});
});

//List open and closed
function hide_show_group(e){
	e.preventDefault();
	if ($(e.currentTarget).hasClass('active'))
	{
		$(e.currentTarget).parent().find('div.list-main-body').hide();
		$(e.currentTarget).removeClass('active');
	}
	else
	{
		$(e.currentTarget).parent().find('div.list-main-body').show();
		$(e.currentTarget).addClass('active');
	}
	return false;
}
function add_task_with_group(new_task, tr, group)
{
	var ng = $('#tmpl_new_group_task').tmpl(group);
	$('#tmpl_new_task_in_group').tmpl(new_task).insertAfter(ng.find('table tr:first'));
	ng.insertAfter('div.list:first');
	$('div.list:first').next().find('table tr:first').next().bind_process_task_row(new_task);
	$('div.list:first').next().add_new_task_layout();
}
function add_new_task_in_group(new_task, tr, group_id)
{
	$('#tmpl_new_task_in_group').tmpl(new_task).insertAfter(tr.parents('.tstyle_content').find('.list.'+group_id).find('tr:first'));
	tr.parents('.tstyle_content').find('.list.'+group_id).find('tr:first').next().bind_process_task_row();
}
function add_new_task(new_task, tr, newList, targetList){
	if (newList == undefined && targetList == undefined)
	{
		var n_task = new Object();
		tasks.push(new_task);
		$('#tmpl_new_task_in_group').tmpl(new_task).insertAfter(tr.parents('table').find('tr:first'));
		tr.parents('table').find('tr:first').next().bind_process_task_row();
	}
	if (newList != undefined )
	{
		var ng = $('#tmpl_new_group_task').tmpl();
		$('#tmpl_new_task_in_group').tmpl(new_task).insertAfter(ng.find('table tr:first'));
		ng.insertAfter('div.list:first');
		$('div.list:first').next().find('table tr:first').next().bind_process_task_row();
		$('div.list:first').next().add_new_task_layout();			                                                        	
	}
	if (targetList != undefined)
	{
		$('#tmpl_new_task_in_group').tmpl(new_task).insertAfter(tr.parents('.tstyle_content')).find('.list-'+targetList).find('tr:first');
		tr.parents('.tstyle_content').find('.list-'+targetList).find('tr:first').next().bind_process_task_row();
	}
}
function convert_status(key)
{
	var text = '';
	switch (key)
	{
		case 10:
			text = 'Done';
			break;
		case 11:
			text = 'On progress';
			break;
		case 12:
			text = 'Suspended';
			break;
		case 13:
			text = 'Late';
			break;
		default:
			break;
	}
	return text;
}
function make_task(tasks)
{
	for (var i = 0; i <tasks.length;  i++)
	{
		var $tr = $('tr.jsmark-'+tasks[i].id);
		$tr.data('task', tasks[i]);
	}
}
(function( $ ){
	$.fn.add_group_task = function(project_id) {
		
		return this.each(function() {
			var $div = $(this);
			var $ele = $div.parent().find('div.sliding');
			new_group($div);
			prepare_group_operation($div);
			$div.find('div.options a').click(function(event){
				event.preventDefault();
				if (!($ele.css('display') == 'none'))
				{
					$ele.fadeOut('slow');
					return false;
				}
				else
				{
					$ele.fadeIn('fast');
				}
				return false;
			});
		});
		function prepare_group_operation($div)
		{
			$div.find('.sliding ul li input[type="checkbox"]').click(function(e){
				e.stopPropagation();
				console.log($(this).is(':checked'));
				if ($(this).is(':checked'))
				{
					console.log(1);
					$div.find('.sliding ul li input[type="checkbox"]').attr('checked',false);
					$(this).attr('checked', true);
					$div.find('.sliding ul li input[type="text"]').attr('disabled', true);
					$div.find('.trigger_x input[name="group_name"]').val($(this).parents('li').find('span').html());
				}
				else
				{
					//$(this).attr('checked', false);
					$div.find('.sliding ul li input[type="text"]').attr('disabled', false);
					$div.find('.trigger_x input[name="group_name"]').attr('value','');
				}
			});
		}
		function new_group($div)
		{
			$div.find('.sliding ul li input[type="text"]').keydown(function(e){
				var keycode = e.keyCode;
				if(keycode == '13'){
					if ($(this).val() == '')
						return false;
					var g_name = $(this).val();
					var user_id = $(this).parents('form').find('input[name="user_id"]').val();
					$.ajax({
						url: '/ajax/task/add_group/'+project_id+'?name='+g_name+'&user_id='+user_id,
						success: function(ret) {
							ret = $.parseJSON(ret);
							if (ret.success)
							{
								$div.find('.sliding ul li.add-new').before('<li><input class="al-made" name="list_id" type="checkbox" value="'+
										ret.data.id
										+'"><span>'+ret.data.name+'</span></li>');
							}					
						},
						error: function() {
						},
						complete: function() {
							remove_event($div);
							prepare_group_operation($div);
							$div.find('.sliding ul li input[type="checkbox"]').attr('checked',false);
							
							var $new_li = $div.find('.sliding ul li.add-new').prev();
							$new_li.find('input[type="checkbox"]').attr('checked', true);
							$div.find('.sliding ul li input[type="text"]').attr('disabled', true);
							$div.find('.trigger_x input[name="group_name"]').val($new_li.find('span').html());
						}
					});	
					return false;
				}
			});
		}
		function remove_event($div)
		{
			$div.find('.sliding ul li.input[type="checkbox"]').unbind('click');
		}
	};
})( jQuery );
